package com.jstyle.jclife.fragment;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.widget.Toast;


import com.jstyle.blesdk.Util.SingleDealData;
import com.jstyle.blesdk.cmdenum.SendCmdState;
import com.jstyle.blesdk.model.SendData;
import com.jstyle.jclife.R;
import com.jstyle.jclife.activity.TrackActivity;
import com.jstyle.jclife.ble.BleManager;
import com.jstyle.jclife.view.LoadingDialog;

import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2017/3/22.
 */

public class BaseFragment extends Fragment {

    private ProgressDialog progressDialog;
    private LoadingDialog loadingDialog;
    private BluetoothAdapter mBluetoothAdapter;
    // private LoadingDialog loadingDialog;
    private int REQUEST_ENABLE_BT=0;
    protected void startActivity(Class activity){
        Intent intent =new Intent(getActivity(),activity);
        startActivity(intent);
    }
    protected void startActivity(Class activity,String key,String value){
        Intent intent =new Intent(getActivity(),activity);
        intent.putExtra(key,value);
        startActivity(intent);
    }
    protected void startActivity(Class activity,String key,int value){
        Intent intent =new Intent(getActivity(),activity);
        intent.putExtra(key,value);
        startActivity(intent);
    }
    protected void startActivity(Class activity,int value,boolean isChina){
        Intent intent =new Intent(getActivity(),activity);
        intent.putExtra(TrackActivity.key_ExerciseMode,value);
        intent.putExtra(TrackActivity.key_isChina,isChina);
        startActivity(intent);
    }
    protected void showToast(String text){
        Toast.makeText(getActivity(),text, Toast.LENGTH_SHORT).show();
    }
    protected void showProgressDialog(String message){

        if(progressDialog==null){
            progressDialog=new ProgressDialog(getActivity(),R.style.appprogressdialog);
        }
        if(progressDialog.isShowing())return;
        progressDialog.setMessage(message);
        progressDialog.show();
    }
    protected  void disMissProgressDialog(){
        if(progressDialog!=null&&progressDialog.isShowing()){
            progressDialog.dismiss();
        }
    }

    protected boolean sendData(SendCmdState sendCmdState, SendData sendData){
        if(!BleManager.getInstance().isConnected()){
         //   showToast(getString(R.string.pair_device));
            return false;
        }
        if(sendData==null){
            BleManager.getInstance().writeValue(SingleDealData.sendData(sendCmdState));
        }else {
            BleManager.getInstance().writeValue(SingleDealData.sendData(sendCmdState, sendData));
        }
        return true;

    }
    protected boolean sendData(SendCmdState sendCmdState){
        return  sendData(sendCmdState,null);
    }
//    protected void showLoadingDialog(){
//        if(loadingDialog==null){
//            loadingDialog= new LoadingDialog(getActivity(), R.style.jstyleDialog_style);
//        }
//        if(!loadingDialog.isShowing()) loadingDialog.show();
//    }
//    protected void disMissLoadingDialog(){
//        if(loadingDialog!=null&&loadingDialog.isShowing()){
//            loadingDialog.dismiss();
//        }
//
//    }
    public void shoot(){

    }
    protected void showLoadingDialog(){
        if(loadingDialog==null){
            loadingDialog= new LoadingDialog(getActivity(), R.style.jstyleDialog_style);
        }
        if(!loadingDialog.isShowing()) loadingDialog.show();
    }
    protected void disMissLoadingDialog(){
        if(loadingDialog!=null&&loadingDialog.isShowing()){
            loadingDialog.dismiss();
        }

    }

    protected void unSubscribe(Disposable subscription) {
        if (subscription != null && !subscription.isDisposed()) {
            subscription.dispose();
        }
    }
    public void updateDate(String date,int dataType){

    }
    protected boolean offerData(SendCmdState sendCmdState){
        return  offerData(sendCmdState,null);
    }
    protected void offerData(byte[]value){
        BleManager.getInstance().offerValue(value);
    }
    protected void offerData(){
        BleManager.getInstance().writeValue();
    }
    protected boolean offerData(SendCmdState sendCmdState, SendData sendData){
        if(!BleManager.getInstance().isConnected()){
        //    showToast(getString(R.string.pair_device));
            return false;
        }
        if(sendData==null){
            BleManager.getInstance().offerValue(SingleDealData.sendData(sendCmdState));
        }else {
            BleManager.getInstance().offerValue(SingleDealData.sendData(sendCmdState, sendData));
        }
        return true;

    }
//    protected boolean isJstyleFlavor(){
//        return BuildConfig.FLAVOR.equals("jstyle");
//    }
protected void enableBle(){
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
    protected boolean isBleEnable(){
        final BluetoothManager bluetoothManager =
                (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
        if(bluetoothManager==null)return false;
        mBluetoothAdapter = bluetoothManager.getAdapter();
        return mBluetoothAdapter!=null&& mBluetoothAdapter.isEnabled();
    }
    public void switchAccountType(boolean isPhoneAccount){

    }
    protected void startActivity(Class activity, String key, boolean value) {
        Intent intent = new Intent(getActivity(), activity);
        intent.putExtra(key, value);
        startActivity(intent);
    }

}
